// pages/cash_package/index/index.js
var app = getApp(), cutTime;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgurl: app.urlimg,

    // 模态框
    model_box: false,
    activity: true,

    //规则内容
    rule_box: false,
    rule_center: [],

    //活动状态提示
    activity_status: null,
    // activity_status: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    that.Load_fun(that);

  },
  //活动状态请求封装函数
  Load_fun: function (that) {

    // wx.request({
    //   url: '',
    //   data: '',
    //   header: {},
    //   success: function(res) {

    //伪数据
    var res = [];
    //规则
    res.rule_center = ['规则1规则1规则1规则1规则1规则1规则1', '规则2规则2规则2规则2规则2规则2规则2规则2规则2', '规则3规则3规则3规则3',];
    //活动信息（once已拆开团数，remaining剩余团数，people_num缺少的人数，momey红包金额）
    res.initiative = { id: '01', once: '40000', remaining: '10000', people_num: 8, momey: 50, };
    //参团人员
    res.join_user = [
      { name: '小墨', image: 'upload/wx/luckmoney/touxiang.png', momey: '1.00', },
      { name: '小墨', image: 'upload/wx/luckmoney/touxiang.png', momey: '2.00', },
      { name: '小墨', image: 'upload/wx/luckmoney/touxiang.png', momey: '10.00', },
      { name: '小墨', image: 'upload/wx/luckmoney/touxiang.png', momey: '5.00', },
      { name: '小墨', image: 'upload/wx/luckmoney/touxiang.png', momey: '7.00', },
    ];
    //倒计时（时间戳）
    res.time_s = 6000000;
    //null:初始状态，0:开启成功，1：瓜分成功，2：好友进入4：超出活动允许拆红包的次数，5：达到发起红包上限次数，6：活动过期
    res.activity_status = 2;

    //判断最佳手气
    if (res.join_user) {
      var lucks = res.join_user;
      var all_dol = [];
      for (var i = 0; i < lucks.length; i++) {
        all_dol.push(lucks[i].momey);
      }
      console.log(max_dol(all_dol));
      that.setData({
        invite: max_dol(all_dol),
      })
    }
    function max_dol(all_num) {
      var max_index = all_num[0];
      for (var i = 1; i < all_num.length; i++) {
        if (parseInt(all_num[i]) > parseInt(max_index)) {
          max_index = all_num[i];
        }
      }
      return max_index;
    }


    if (res.activity_status == 0 || res.activity_status == 2) {//开启成功
      that.setData({
        model_box: true,
        time_s: res.time_s,//倒计时时间(时间戳)
      });
      that.time(that.data.time_s);
      that.onShow();
    } else if (res.activity_status == 1) {//瓜分成功
    } else if (res.activity_status == 4 || res.activity_status == 5 || res.activity_status == 6) {//4：超出活动允许拆红包的次数，5：达到发起红包上限次数，6：活动过期
      that.setData({
        model_box: true,
      });
    }
    that.setData({
      activity_status: res.activity_status,
      join_user: res.join_user,
      rule_center: res.rule_center,
      initiative: res.initiative,
    })

    //   },
    // })
  },
  //查看规则
  rule_click: function (e) {
    var that = this;
    var activity_status = that.data.activity_status;
    if (activity_status == 5 || activity_status == 6) {
      wx.reLaunch({
        url: '/pages/index/index',
      })
      return;
    }
    if (!that.data.model_box) {
      that.setData({ model_box: true, rule_box: true, activity: false, })
    } else {
      that.setData({ model_box: false, rule_box: false, activity: false, })
    }
  },
  //开启红包
  open_activity: function (e) {
    var that = this;
    that.Load_fun(that);

  },
  // 获得毫秒数  转化  成小时、分钟、秒
  time: function (Time) {
    var that = this;
    Time -= 1;
    if (Time > -1) {

      var hours = parseInt(Time / 60 / 60 % 24, 10); //计算剩余的小时 
      hours = item_fun(hours);
      var minutes = parseInt(Time / 60 % 60, 10);//计算剩余的分钟 
      minutes = item_fun(minutes);
      var seconds = parseInt(Time % 60, 10);//计算剩余的秒数 
      seconds = item_fun(seconds);

      var times = hours + ":" + minutes + ":" + seconds;//排列时间

      // console.log(times);
      that.setData({
        countdown: times,
        time_s: Time,
      })

      function item_fun(item_num) {
        if (('' + item_num).length == 1) {
          return ('0' + item_num);
        } else {
          return item_num;
        }
      }
    }
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.data.activity_status == 0) {
      cutTime = setInterval(function () {
        if (that.data.time_s >= 1) {
          that.time(that.data.time_s);
        } else {
          that.setData({
            activity_status: 5
          });
          clearInterval(cutTime);
        }
      }, 1000);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    clearInterval(cutTime);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    clearInterval(cutTime);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})